import myAxios from "@/plugins/axios";
import {ApiData, MyComment, MyFavorite, MyTopic, PageData, UserModel} from "@/interfaces/apiResponse.ts";
import myAuth from "@/composables/myAuth.ts";
import {ElMessageBox} from "element-plus";

export default () => {
    const user = ref<UserModel>();
    const users = ref<PageData<UserModel>>();
    const topics = ref<PageData<MyTopic>>();
    const comments = ref<PageData<MyComment>>();
    const is_favorite = ref<boolean>(false);
    const getCurrentUser = async () => {
        const res = await myAxios.request<ApiData<UserModel>>({
            url: `get_current_user`
        });
        return res.data;
    }
    const getOneUser = async (u_id: any) => {
        const res = await myAxios.request<ApiData<UserModel>>({
            url: `user/${u_id}`
        });
        user.value = res.data;
    }
    const getOneUserTopics = async (arg: Record<string, any>) => {
        //{id:1,name:Jack}
        //key=abc&keywords=bbb
        const paramsStr = Object.entries(arg).map(e => e.join('=')).join('&');
        topics.value = await myAxios.request<PageData<MyTopic>>({
            url: `get_one_user_topics?${paramsStr}`
        });
    }
    const getOneUserComments = async (arg: Record<string, any>) => {
        //{id:1,name:Jack}
        //key=abc&keywords=bbb
        const paramsStr = Object.entries(arg).map(e => e.join('=')).join('&');
        comments.value = await myAxios.request<PageData<MyComment>>({
            url: `get_one_user_comments?${paramsStr}`
        });
    }
    const getIsFavorite = async (u_id: any, t_id: any) => {
        const res = await myAxios.request<ApiData<MyFavorite>>({
            url: `is_favorite/${u_id}/${t_id}`
        });
        is_favorite.value = res.data.is_favorite;
    }
    const toggleFavorite = async (t_id: any) => {
        //favorite/toggle/{id}
        await myAxios.request<ApiData<any>>({
            url: `favorite/toggle/${t_id}`
        });
    }
    const getOneUserFavoriteTopics = async (arg: Record<string, any>) => {
        //{id:1,name:Jack}
        //key=abc&keywords=bbb
        const paramsStr = Object.entries(arg).map(e => e.join('=')).join('&');
        topics.value = await myAxios.request<PageData<MyTopic>>({
            url: `get_one_user_favorite_topics?${paramsStr}`
        });
    }
    const updateCurrentUser = async (data: any) => {
        await myAxios.request<ApiData<UserModel>>({
            url: `update_current_user`,
            method: 'PUT',
            data
        });
        await myAuth().logout();
    }
    const updateCurrentUserNick = async (data: any) => {
        await myAxios.request<ApiData<UserModel>>({
            url: `update_current_user_nick`,
            method: 'PUT',
            data
        });
        await myAuth().logout();
    }
    const getUsers = async (arg: Record<string, any>) => {
        //{id:1,name:Jack}
        //key=abc&keywords=bbb
        const paramsStr = Object.entries(arg).map(e => e.join('=')).join('&');
        users.value = await myAxios.request<PageData<UserModel>>({
            url: `get_users?${paramsStr}`
        });
    }
    const switchFreeze = async (u_id: any,type:any) => {
        const str = type == 'yes' ? '解冻' : '冻结';
        await  ElMessageBox.confirm(
            `您确认要${str}用户吗?`,
            '删除提示',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
        await myAxios.request<ApiData<null>>({
            url: `user_freeze/${u_id}`,
            method: 'PUT',
        });
        location.reload();
    }
    const delUser = async (u_id:any)=>{
        try {
            await  ElMessageBox.confirm(
                '您确认要删除该用户吗?',
                '删除提示',
                {
                    confirmButtonText: '确认',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            )
            await myAxios.request<ApiData<null>>({
                url:`user_del/${u_id}`,
                method:'DELETE',
            })
            location.reload();
        }catch (error){

        }
    }
    return {
        getCurrentUser, user, getOneUser, topics,
        getOneUserTopics, comments, getOneUserComments, is_favorite,
        getIsFavorite, toggleFavorite, getOneUserFavoriteTopics, updateCurrentUser, users, getUsers,updateCurrentUserNick,
        switchFreeze,delUser
    };
}